﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;

namespace ServiceOperater.Helpers
{
    //--
    // 居然有同名的 DependencyPropertyHelper 类
    public sealed class DependencyPropertyUtil
    {
        //  https://www.infragistics.com/community/blogs/blagunas/archive/2013/01/29/get-dependencyproperty-by-name-for-wpf-and-silverlight.aspx
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dependencyObject"></param>
        /// <param name="dpName">必须是以Property,例如的TextBox的TextProperty; 因为我们是要检索DP</param>
        /// <returns></returns>
        public static DependencyProperty GetDependencyPropertyByName(DependencyObject dependencyObject, string dpName)
        {
            return GetDependencyPropertyByName(dependencyObject.GetType(), dpName);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dependencyObjectType"></param>
        /// <param name="dpName">必须是以Property,例如的TextBox的TextProperty; 因为我们是要检索DP</param>
        /// <returns></returns>
        public static DependencyProperty GetDependencyPropertyByName(Type dependencyObjectType, string dpName)
        {
            DependencyProperty dp = null;

            var fieldInfo = dependencyObjectType.GetField(dpName, BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);
            if (fieldInfo != null)
            {
                dp = fieldInfo.GetValue(null) as DependencyProperty;
            }

            return dp;
        }

        // ------------------------------ Self
        public static Object getDependencyPropertyVal(DependencyObject dependencyObject, string dpName)
        {
            var dp = GetDependencyPropertyByName(dependencyObject, dpName);
            if (dp == null)
            {
                return null;
            }

            return dependencyObject.GetValue(dp);
        }

        public static void SetDependencyPropertyVal(DependencyObject dependencyObject, string dpName, Object val)
        {
            var dp = GetDependencyPropertyByName(dependencyObject, dpName);
            if (dp == null)
            {
                return;
            }

            dependencyObject.SetValue(dp, val);
        }

        // 失败, DependencyProperty里的HashTable类型的私有静态字段PropertyFromName保存着所有的依赖属性,但其key为FromNameKey类型, 而这个FromNameKey类型是DependencyProperty中的私有内部类, 坑啊

    //    public static DependencyProperty GetDPByName(DependencyObject dependencyObject, string dpName)
    //    {
    //        Type t = typeof(DependencyProperty)
    //.Assembly.GetType("System.Windows.DependencyProperty.FromNameKey");
    //        ConstructorInfo ctor = t.GetConstructors
    //        (BindingFlags.Instance | BindingFlags.NonPublic)[0];
    //        var fromNameKey = ctor.Invoke(new object[] { dpName, dependencyObject });
    //        var fieldInfo = (typeof(DependencyObject).GetField("PropertyFromName", BindingFlags.Static | BindingFlags.NonPublic));
    //        // 获取静态字段的值,传入null
    //        var PropertyFromName = (Hashtable)fieldInfo.GetValue(null);
    //        return (DependencyProperty)PropertyFromName[fromNameKey];
    //    }
    }
}
